<?php

class BatchProm {
   
    private $conx;
    private $growth = "SELECT max(growthucod) FROM GROWTH_USER";
    private $growthnum;
    private $cod = "select c.batchcod, b.landcod, l.landcod, l.userid, c.dateregister from cattle as c, land as l, batch as b where  l.userid='%s' and c.dateregister='%s' and b.landcod=l.landcod"; 
    private $c;
    private $wBirth = "select AVG(weightbirth) from cattle where batchcod='%s' and dateregister='%s'";
    private $wBirthp;
    private $wbeforeWeaning = "select AVG(weightact) from cattle where batchcod='%s' and dateregister='%s' and stage = 'Predestete'";
    private $wbw;
    private $wWeanig = "select AVG(weightweaning) from cattle where batchcod='%s' and dateregister='%s'";
    private $ww;
    private $wPC = "select AVG(weightact) from cattle where batchcod='%s' and dateregister='%s' and stage = 'PreCeba'";
    private $wpcp;
    private $wC = "select AVG(weightact) from cattle where batchcod='%s' and dateregister='%s' and stage = 'Ceba'";
    private $wcp;
    private $agebeforeWeaning = "select AVG(ageact) from cattle where batchcod='%s' and dateregister='%s' and stage = 'Predestete'";
    private $abw;
    private $ageWeaning = "select AVG(ageweaning) from cattle where batchcod='%s' and dateregister='%s'";
    private $aw;
    private $agePC = "select AVG(ageact) from cattle where batchcod='%s' and dateregister='%s' and stage = 'PreCeba'";
    private $apc;
    private $ageC = "select AVG(ageact) from cattle where batchcod='%s' and dateregister='%s' and stage = 'Ceba'";
    private $ac;


    public function __construct() {
        $this->conx = null;
    }
    
    public function growth(){
        $consult = sprintf($this->growth);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        $date = pg_fetch_assoc($result);
        $val = $date['max'];
        if($val == " "){
            $this->growthnum = 1;
        } else {
            $this->growthnum = $val + 1;
        }
        return $this->growthnum;
    }
    
    public function cod($user, $d){
        $consult = sprintf($this->cod, $user, $d);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        $dat = pg_fetch_row($result);
        $val = $dat[0];
        if($val == " "){
            $this->c = null;
        } else {
            $this->c = $val;
        }
        return $this->c;
    }

    public function wBirth($batch, $date){
        $consult = sprintf($this->wBirth, $batch, $date);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        $dat = pg_fetch_row($result);
        $val = $dat[0];
        if($val == " "){
            $this->wBirthp = null;
        } else {
            $this->wBirthp = $val;
        }
        return $this->wBirthp;
    }
    
    public function wbeforeWeaning($batch, $date){
        $consult = sprintf($this->wbeforeWeaning, $batch, $date);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        $dat = pg_fetch_row($result);
        $val = $dat[0];
        if($val == " "){
            $this->wbw = null;
        } else {
            $this->wbw = $val;
        }
        return $this->wbw;
    }
    
    public function wWeanig($batch, $date){
        $consult = sprintf($this->wWeanig, $batch, $date);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        $dat = pg_fetch_row($result);
        $val = $dat[0];
        if($val == " "){
            $this->ww = null;
        } else {
            $this->ww = $val;
        }
        return $this->ww;
    }
    
    public function wPC($batch, $date){
        $consult = sprintf($this->wPC, $batch, $date);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        $dat = pg_fetch_row($result);
        $val = $dat[0];
        if($val == " "){
            $this->wpcp = null;
        } else {
            $this->wpcp = $val;
        }
        return $this->wpcp;
    }
    
    public function wC($batch, $date){
        $consult = sprintf($this->wC, $batch, $date);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        $dat = pg_fetch_row($result);
        $val = $dat[0];
        if($val == " "){
            $this->wcp = null;
        } else {
            $this->wcp = $val;
        }
        return $this->wcp;
    }
    
    public function agebeforeWeaning($batch, $date){
        $consult = sprintf($this->agebeforeWeaning, $batch, $date);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        $dat = pg_fetch_row($result);
        $val = $dat[0];
        if($val == " "){
            $this->abw = null;
        } else {
            $this->abw = $val;
        }
        return $this->abw;
    }
    
    public function ageWeaning($batch, $date){
        $consult = sprintf($this->ageWeaning, $batch, $date);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        $dat = pg_fetch_row($result);
        $val = $dat[0];
        if($val == " "){
            $this->aw = null;
        } else {
            $this->aw = $val;
        }
        return $this->aw;
    }
    
    public function agePC($batch, $date){
        $consult = sprintf($this->agePC, $batch, $date);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        $dat = pg_fetch_row($result);
        $val = $dat[0];
        if($val == " "){
            $this->apc = null;
        } else {
            $this->apc = $val;
        }
        return $this->apc;
    }
    
    public function ageC($batch, $date){
        $consult = sprintf($this->ageC, $batch, $date);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        $dat = pg_fetch_row($result);
        $val = $dat[0];
        if($val == " "){
            $this->ac = null;
        } else {
            $this->ac = $val;
        }
        return $this->ac;
    }
        
}

?>
